﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.BU;
using Com.DA;
using ColdStorage.DOL;
using System.Media;
using System.Windows.Controls.Primitives;

namespace ColdStorage.GUI
{
    /// <summary>
    /// Interaction logic for ucInternalLoadCold.xaml
    /// </summary>
    public partial class ucInternalLoadCold : UserControl
    {
        public int Product_type;
        public int Product_id;
        public int ProductStorageTemp;
        public int ProductInputTemp;
        public int RoomTypeId;
        public double RoomArea;
        public string key = "";
        InternalLoad inter;

        public ucInternalLoadCold()
        {
            InitializeComponent();
            loadInternalInfo();
        }

        #region PROCESS INTERNAL LOAD
        private void radIn_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            this.lblPerform.IsEnabled = false;
            this.cbxPerformance.IsEnabled = false;
            btnApplyInternalLoad.IsEnabled = true;
        }

        private void RadOut_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            this.lblPerform.IsEnabled = true;
            this.cbxPerformance.IsEnabled = true;
            btnApplyInternalLoad.IsEnabled = true;
        }

        public void loadInternalInfo()
        {
            txtPeopleNumber.Text = "2";

            //Công suất máy
            cbxCapacity.Items.Clear();
            for (int i = 1; i <= 16; i++)
            {
                cbxCapacity.Items.Add(i);
            }

            //Hiệu suất máy
            cbxPerformance.Items.Clear();
            double dbPerformance = 45;
            for (int j = 0; j < 7; j++)
            {
                dbPerformance += 5;
                cbxPerformance.Items.Add(dbPerformance);
            }
            cbxPerformance.Text = "0";

            //Nhiệt lượng tỏa ra khi chiếu sáng 1m2
            cbxLightingHeat.Items.Clear();
            cbxLightingHeat.Items.Add("1.2");
            cbxLightingHeat.Items.Add("4.5");

            //Entanpi của không khí
            txtEnthalpyAirInside.Clear();
            txtEnthalpyAirOurside.Clear();
            txtEnthalpyAirInside.Text = "15";
            txtEnthalpyAirOurside.Text = "126";

            //Các hệ số do thông gió
            double dbMultipleCycle = 0;
            cbxVentilateMultipleCycle.Items.Clear();
            for (int k = 0; k < 2; k++)
            {
                dbMultipleCycle += 5;
                cbxVentilateMultipleCycle.Items.Add(dbMultipleCycle);
            }

            cbxVentilateAirDensity.Items.Clear();

            double dbAirDensity = 1.3;

            for (int l = 1; l < 7; l++)
            {
                dbAirDensity += 0.1;
                cbxVentilateAirDensity.Items.Add(Math.Round(dbAirDensity, 1));
            }            
        }

        public void Respiration()
        {
            if (Product_type == 1)
            {
                txtRespirationInput.IsEnabled = false;
                txtRespirationStorage.IsEnabled = false;
                lblRespirationInput.IsEnabled = false;
                lblRespirationStorage.IsEnabled = false;
                lblFruitRespiration.IsEnabled = false;
            }
            else
            {
                if (Product_type == 2)
                {
                    txtRespirationInput.IsEnabled = true;
                    txtRespirationStorage.IsEnabled = true;
                    lblRespirationInput.IsEnabled = true;
                    lblRespirationStorage.IsEnabled = true;
                    lblFruitRespiration.IsEnabled = true;
                    tb_respiratory_heat rh = LibraryManagerInCom.RespirationList.SingleOrDefault
                       (s => s.product_id == Product_id && s.temperature == ProductInputTemp);

                    tb_respiratory_heat rh2 = LibraryManagerInCom.RespirationList.SingleOrDefault
                       (s => s.product_id == Product_id && s.temperature == ProductStorageTemp);
                    if (rh != null)
                    {
                        txtRespirationInput.Text = rh.heat.ToString();
                    }
                    if (rh2 != null)
                    {
                        txtRespirationStorage.Text = rh2.heat.ToString();
                    }
                }
            }
        }

        public void HeatDoorOpening()
        {
            try
            {
                if (RoomArea <= 50)
                {
                    tb_storage_room_type rt = LibraryManagerInCom.RoomTypeList.SingleOrDefault
                        (s => s.id == RoomTypeId);
                    txtHeatDoorOpening.Text = rt.C50m2.ToString();
                }
                if (RoomArea > 50 && RoomArea <= 150)
                {
                    tb_storage_room_type rt = LibraryManagerInCom.RoomTypeList.SingleOrDefault
                        (s => s.id == RoomTypeId);
                    txtHeatDoorOpening.Text = rt.C50_150m2.ToString();
                }
                if (RoomArea > 150)
                {
                    tb_storage_room_type rt = LibraryManagerInCom.RoomTypeList.SingleOrDefault
                        (s => s.id == RoomTypeId);
                    txtHeatDoorOpening.Text = rt.C_150m2.ToString();
                }
            }
            catch (Exception)
            {

            }
        }

        #endregion

        private void btnApplyInternalLoad_Click(object sender, RoutedEventArgs e)
        {
            AppInternal();
        }

        public void AppInternal()
        {
            if (Product_type == 1)
            {
                txtRespirationInput.Text = "0";
                txtRespirationStorage.Text = "0";
            }
            HeatDoorOpening();
            if (RadLocationOut.IsChecked == true)
            {
                inter.dbEnginePerformance = Convert.ToDouble(cbxPerformance.Text);
            }
            else
            {
                inter.dbEnginePerformance = 0;
            }
            inter.dbPeopleNumber = Convert.ToInt32(txtPeopleNumber.Text);
            inter.dbEngineCapacity = Convert.ToDouble(cbxCapacity.Text);
            inter.dbLightHeat = Convert.ToDouble(cbxLightingHeat.Text);
            inter.dbDoorHeatFlowOpening = Convert.ToDouble(txtHeatDoorOpening.Text);
            inter.dbVentilateMultiplecycle = Convert.ToDouble(cbxVentilateMultipleCycle.Text);
            inter.dbAirDensity = Convert.ToDouble(cbxVentilateAirDensity.Text);
            inter.dbAirEnthalpyInside = Convert.ToDouble(txtEnthalpyAirInside.Text);
            inter.dbAirEnthalpyOutside = Convert.ToDouble(txtEnthalpyAirOurside.Text);
            inter.dbRespirationHeatFlowInput = Convert.ToDouble(txtRespirationInput.Text);
            inter.dbRespirationHeatFlowStorage = Convert.ToDouble(txtRespirationStorage.Text);
            btnApplyInternalLoad.IsEnabled = false;
        }

        public void loadInternal(InternalLoad i)
        {
            if (inter != null)
            {
                AppInternal();
            }
            inter = i;
            txtPeopleNumber.Text = inter.dbPeopleNumber.ToString();
            cbxCapacity.Text = inter.dbEngineCapacity.ToString();
            if (inter.dbEnginePerformance != 0)
            {
                RadLocationOut.IsChecked = true;
                cbxPerformance.Text = inter.dbEnginePerformance.ToString();
            }
            else
            {
                radLocationIn.IsChecked = true;
            }

            cbxLightingHeat.Text = inter.dbLightHeat.ToString();
            txtHeatDoorOpening.Text = inter.dbDoorHeatFlowOpening.ToString();
            cbxVentilateMultipleCycle.Text = inter.dbVentilateMultiplecycle.ToString();
            cbxVentilateAirDensity.Text = inter.dbAirDensity.ToString();
            txtEnthalpyAirInside.Text = inter.dbAirEnthalpyInside.ToString();
            txtEnthalpyAirOurside.Text = inter.dbAirEnthalpyOutside.ToString();
            txtRespirationInput.Text = inter.dbRespirationHeatFlowInput.ToString();
            txtRespirationStorage.Text = inter.dbRespirationHeatFlowStorage.ToString();
        }

        #region CHECK EMPTY
        public void CheckEmpty(TextBox txt)
        {
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.Contains(' '))
            {
                txt.Text = txt.Text.Remove(txt.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        private void txtPeopleNumber_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtPeopleNumber);
        }

        private void txtHeatDoorOpening_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtHeatDoorOpening);
        }

        private void txtEnthalpyAirInside_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtEnthalpyAirInside);
        }

        private void txtEnthalpyAirOurside_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtEnthalpyAirOurside);
        }

        private void txtRespirationInput_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtRespirationInput);
        }

        private void txtRespirationStorage_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtRespirationStorage);
        }

        private void cbxCapacity_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            btnApplyInternalLoad.IsEnabled = true;
        }

        private void cbxPerformance_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            btnApplyInternalLoad.IsEnabled = true;
        }

        private void cbxLightingHeat_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            btnApplyInternalLoad.IsEnabled = true;
        }

        private void cbxVentilateMultipleCycle_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            btnApplyInternalLoad.IsEnabled = true;
        }

        private void cbxVentilateAirDensity_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            btnApplyInternalLoad.IsEnabled = true;
        }
        #endregion

        #region CHECK INPUT
        private void txtInput_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
                btnApplyInternalLoad.IsEnabled = true;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (sender is TextBox)
                    {
                        if (!((TextBox)sender).Text.Contains("."))
                        {
                            e.Handled = false;
                            btnApplyInternalLoad.IsEnabled = true;
                        }
                        else
                        {
                            e.Handled = true;
                            SystemSounds.Beep.Play();
                        }
                    }
                    else
                    {
                        if (sender is ComboBox)
                        {
                            if (!((ComboBox)sender).Text.Contains("."))
                            {
                                e.Handled = false;
                                btnApplyInternalLoad.IsEnabled = true;
                            }
                            else
                            {
                                e.Handled = true;
                                SystemSounds.Beep.Play();
                            }
                        }
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }

        private void cbxCapacity_TextChanged(object sender, TextChangedEventArgs e)
        {
            ComboBox_TextChanged(cbxCapacity);
        }

        private void cbxLightingHeat_TextChanged(object sender, TextChangedEventArgs e)
        {
            ComboBox_TextChanged(cbxLightingHeat);
        }

        private void cbxVentilateMultipleCycle_TextChanged(object sender, TextChangedEventArgs e)
        {
            ComboBox_TextChanged(cbxVentilateMultipleCycle);
        }

        private void cbxVentilateAirDensity_TextChanged(object sender, TextChangedEventArgs e)
        {
            ComboBox_TextChanged(cbxVentilateAirDensity);
        }

        private void cbxPerformance_TextChanged(object sender, TextChangedEventArgs e)
        {
            ComboBox_TextChanged(cbxPerformance);
        }

        public void ComboBox_TextChanged(ComboBox com)
        {
            if (com.Text == "")
            {
                com.Text = "0";
            }
            if (com.Text.Contains(' '))
            {
                com.Text = com.Text.Remove(com.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (com.Text.First() == '.')
            {
                com.Text = com.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }
        #endregion        

        #region GOT FOCUS
        private void txtInput_GotFocus(object sender, RoutedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            key = txt.Name;
        }

        private void txtInput_Loaded(object sender, RoutedEventArgs e)
        {
            ToolTip tt = new ToolTip();
            TextBox txt = (TextBox)sender;
            ToolTipService.SetPlacement(tt, PlacementMode.Left);
            tt.Content = "Press F1 for Help";
            txt.ToolTip = tt;
        }
        #endregion
    }
}
